package com.ayoka.parenting.ui.meditation.index;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.MeditationBean;
import com.ayoka.parenting.bean.MeditationIndexDataBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.FragmentMeditationBinding;
import com.ayoka.parenting.databinding.ItemMeditationIndexBinding;
import com.ayoka.parenting.mvp.MVPBaseFragment;
import com.ayoka.parenting.ui.meditation.detail.MeditationDetailActivity;
import com.ayoka.parenting.ui.meditation.more.MeditationMoreActivity;
import com.ayoka.parenting.ui.payment.openmember.OpenMemberActivity;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.StringUtil;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationIndexFragment extends MVPBaseFragment<FragmentMeditationBinding, MeditationIndexContract.View, MeditationIndexPresenter> implements MeditationIndexContract.View {

    private MeditationIndexDataBean meditationIndexDataBean;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_meditation;
    }

    @Override
    protected void initViews(View view) {
        ImageLoader.getInstance().displayBitmap(R.mipmap.image_meditation_index_blur, mDataBinding.ivBannerBlur);
        ImageLoader.getInstance().displayBitmap(R.mipmap.image_meditation_index, mDataBinding.ivBannerFront);

        mDataBinding.tvMore.setOnClickListener(this);
        mDataBinding.btnOpenMember.setOnClickListener(this);
        if (UserManager.getInstance().detailBean != null && UserManager.getInstance().detailBean.user_info != null) {
            if (UserManager.getInstance().detailBean.user_info.IsCoach == 0) {
                if (UserManager.getInstance().detailBean.user_info.IsMembership != 2) {
                    mDataBinding.btnOpenMember.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.btnOpenMember.setVisibility(View.GONE);
                }
            } else {
                mDataBinding.btnOpenMember.setVisibility(View.GONE);
            }
        }
    }


    @Override
    protected void initData() {
        mPresenter.getMeditationIndexData();
    }

    @Override
    protected MeditationIndexPresenter getPresenter() {
        return new MeditationIndexPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.tvMore) {
            MeditationMoreActivity.start(getActivity());
        } else if (view == mDataBinding.btnOpenMember) {
            OpenMemberActivity.start(getActivity());
        }
    }

    @Override
    public void configMeditationIndexData(MeditationIndexDataBean indexData) {
        if (indexData == null) return;
        this.meditationIndexDataBean = indexData;
        mDataBinding.tvMeditationData.setText(StringUtil.formatString(R.string.format_meditation_data, meditationIndexDataBean.user_counts, meditationIndexDataBean.meditation_counts));
        addMeditationItem();

    }

    private void addMeditationItem() {
        mDataBinding.layoutMeditationContainer.removeAllViews();
        if (meditationIndexDataBean.meditation_list == null) return;
        for (MeditationBean meditationBean : meditationIndexDataBean.meditation_list) {
            View itemView = getMeditationView(meditationBean);
            if (itemView != null) {
                mDataBinding.layoutMeditationContainer.addView(itemView);
            }
        }
    }

    private View getMeditationView(final MeditationBean meditationBean) {
        if (meditationBean == null) return null;
        ItemMeditationIndexBinding meditationIndexBinding = ItemMeditationIndexBinding.inflate(LayoutInflater.from(getActivity()));
        ImageLoader.getInstance().displayBitmap(meditationBean.SmallImgUrl, meditationIndexBinding.ivPicture, ImageLoaderOptions.getRoundOptions());
        meditationIndexBinding.tvProgress.setText(meditationBean.OrderNum + "/21");
        meditationIndexBinding.tvMeditationTitle.setText(meditationBean.Title);
        meditationIndexBinding.tvMeditationDuration.setText(DateUtil.formatTime(meditationBean.MediaDuration));
        meditationIndexBinding.tvMeditationStudyCount.setText(StringUtil.formatString(R.string.format_study_count, meditationBean.Participant));
        meditationIndexBinding.tvLastStudyLabel.setVisibility(TextUtils.equals(meditationIndexDataBean.last_meditation_id, meditationBean.ID) ? View.VISIBLE : View.GONE);
        meditationIndexBinding.btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MeditationDetailActivity.start(getActivity(), meditationBean.ID, meditationBean.Title, meditationBean.MediaType);
            }
        });
        return meditationIndexBinding.getRoot();
    }
}
